package com.hei.kml.converter;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.hei.kml.Template;
import com.hei.kml.Waylines;

public interface JSONConverter<T> {
	default T fromJSON(String json) {
		Class<?> clazz = this.getClass();
		Object obj;
		if (clazz.equals(Waylines.class)) {
			obj = new Waylines();
			Waylines.Document document = JSONUtil.toBean(
					json, JSONConfig.create().setIgnoreCase(true), Waylines.Document.class);
			((Waylines) obj).setDocument(document);
		} else if (clazz.equals(Template.class)) {
			obj = new Template();
			Template.Document document = JSONUtil.toBean(
					json, JSONConfig.create().setIgnoreCase(true), Template.Document.class);
			((Template) obj).setDocument(document);
		} else {
			throw new RuntimeException("This class cannot be resolved.");
		}
		return (T) obj;
	}

	default String toJSON() {
		return JSONUtil.toJsonStr(this);
	}
}
